﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace LitTools
{
    public abstract class MonoWithFilterQueue : MonoBehaviour
    {
        public bool openLog = false;

        /// <summary>
        /// 过滤器队列
        /// </summary>
        public List<IRaycastResultFilter> FilterQueue = new();

        /// <summary>
        /// 通过过滤器
        /// </summary>
        /// <param name="results"></param>
        /// <returns></returns>
        protected List<RaycastResult> PassFilterQueue(List<RaycastResult> results)
        {
            if (FilterQueue.Count == 0)
            {
#if UNITY_EDITOR
                Debug.Log("FilterQueue is NULL");
#endif
                return results;
            }
#if UNITY_EDITOR
            LogRaycastResult("BeforePass", results);
#endif
            for (int i = 0; i < FilterQueue.Count; i++)
            {
                var filter = FilterQueue[i];
                results = filter.FiltRaycastResults(results);
#if UNITY_EDITOR
                LogRaycastResult($"AfterFilter {filter.GetType()}", results);
#endif
            }
            return results;
        }

        /// <summary>
        /// 过滤结果日志
        /// </summary>
        /// <param name="title"></param>
        /// <param name="results"></param>
        private void LogRaycastResult(string title, List<RaycastResult> results)
        {
            if (!openLog) return;
            string resultStr = "";
            for (int i = 0; i < results.Count; i++)
            {
                resultStr += results[i].gameObject.name;
                if (i < results.Count - 1)
                    resultStr += " ==> ";
            }
            Debug.Log(title + " : " + resultStr);
        }
    }
}